package com.lnking.lnkingplace.controller;


import com.lnking.common.http.ReturnResult;

import com.lnking.lnkingplace.model.dto.inspect.InspectDetailDto;
import com.lnking.lnkingplace.model.dto.inspect.InspectDto;
import com.lnking.lnkingplace.model.dto.inspect.InspectExportDto;
import com.lnking.lnkingplace.model.dto.user.UserDto;
import com.lnking.lnkingplace.model.dto.user.UserExportDto;
import com.lnking.lnkingplace.model.entity.InspectResult;
import com.lnking.lnkingplace.model.entity.Rectification;
import com.lnking.lnkingplace.model.request.inspect.*;
import com.lnking.lnkingplace.model.request.user.UserSearchRequest;
import com.lnking.lnkingplace.service.IInspectService;
import com.lnking.starter.export.utils.ExportUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sn
 * @since 2022-08-20
 */
@Api(tags = "检查方案")
@RestController
@RequestMapping("/inspect")
public class InspectController {

    @Autowired
    IInspectService inspectService;

    @ApiOperation("新增")
    @PostMapping("/add")
    public ReturnResult add(@RequestBody @Valid InspectAddRequest params) throws Exception {
        inspectService.add(params);
        return ReturnResult.success();
    }

    @ApiOperation("编辑场所")
    @PostMapping("/place/add")
    public ReturnResult placeAdd(@RequestBody @Valid InspectAddPlaceRequest params) throws Exception {
        inspectService.placeAdd(params);
        return ReturnResult.success();
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ReturnResult delete(@PathVariable("id") String id) {
        inspectService.delete(id);
        return ReturnResult.success();
    }

    @ApiOperation("编辑")
    @PostMapping("/update")
    public ReturnResult update(@RequestBody @Valid InspectUpdateRequest params) throws Exception {
        inspectService.update(params);
        return ReturnResult.success();
    }

    @ApiOperation("查询列表")
    @GetMapping("/list")
    public ReturnResult getList(InspectSearchRequest params) {
        return ReturnResult.success(inspectService.pageList(params));
    }

    @ApiOperation("提交检查结果")
    @PostMapping("/result")
    public ReturnResult result(@RequestBody InspectResultByPlaceRequest  params) throws Exception {
        inspectService.result(params);
        return ReturnResult.success();
    }

    @ApiOperation("修改检查结果")
    @PostMapping("/update/result")
    public ReturnResult updateResult(@RequestBody List<InspectResult> params) throws Exception {
        inspectService.updateResult(params);
        return ReturnResult.success();
    }

    @ApiOperation("获取检查答题")
    @PostMapping("/get-result-list")
    public ReturnResult getResultList(@RequestBody InspectResultRequest params) throws Exception {
        return ReturnResult.success( inspectService.getResultList(params));
    }

    @ApiOperation("获取详情")
    @GetMapping("/{id}")
    public ReturnResult getDetail(@PathVariable("id") String id) {
        InspectDetailDto dto = inspectService.getInspectDetailById(id);
        return ReturnResult.success(dto);
    }

    @ApiOperation("结果登记--场所列表")
    @GetMapping("/{id}/result")
    public ReturnResult result(@PathVariable("id") String id) {
        return ReturnResult.success( inspectService.getResult(id));
    }


    @ApiOperation("复查登记--场所列表")
    @GetMapping("/{id}/review")
    public ReturnResult review(@PathVariable("id") String id) {
        return ReturnResult.success( inspectService.getReview(id));
    }

    @ApiOperation("结果详情")
    @GetMapping("/result/details")
    public ReturnResult resultDetails( String inspectId  ,String placeId) {
        return ReturnResult.success( inspectService.getResultDetails(inspectId,placeId));
    }

    @ApiOperation("图片修改")
    @PostMapping("/photos")
    public ReturnResult photos(@RequestBody InspectPhotosRequest params) {
        inspectService.photos(params);
        return ReturnResult.success();
    }


    @ApiOperation(value = "导出检查信息")
    @GetMapping(value = "/export")
    public void export(InspectExportRequest params, HttpServletResponse response) throws Exception {
        ExportUtils.export(inspectService.export(params), response, InspectExportDto.class);
    }

    @ApiOperation(value = "导出所有检查信息")
    @GetMapping(value = "/export/info")
    public void exportInfo(InspectSearchRequest params, HttpServletResponse response) throws Exception {
        ExportUtils.export((List<?>) inspectService.pageList(params), response, InspectDto.class);
    }

    @ApiOperation("从指标库添加")
    @PostMapping("/library/add/index")
    public ReturnResult addIndexByLibrary(@RequestBody IndexByLibraryRequest params) {
        inspectService.addIndexByLibrary(params);
        return ReturnResult.success();
    }

    @ApiOperation("录入指标")
    @GetMapping("/writer/add/index")
    public ReturnResult addIndexByWriter(AddIndexRequest params) {
        inspectService.addIndexByWriter(params);
        return ReturnResult.success();
    }

    @ApiOperation("删除指标")
    @GetMapping("/del/index/{inspectResultId}")
    public ReturnResult delIndex(@PathVariable("inspectResultId") String  inspectResultId) {
        inspectService.delIndex(inspectResultId);
        return ReturnResult.success();
    }


}
